﻿using Undercover.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.ServiceLocation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Undercover.ViewModel
{
    public class WordPairViewModel : ViewModelBase
    {
        public WordPairViewModel()
            : this(Goal.CreateNew, null, true)
        {
        }

        public WordPairViewModel(WordPair wordPair, bool canBeAddedToUserLibrary)
            : this(Goal.Modify, wordPair, canBeAddedToUserLibrary)
        {
        }

        public WordPairViewModel(Goal pageType, WordPair wordPair, bool canBeAddedToUserLibrary)
        {
            // assert 
            if (pageType == Goal.CreateNew && wordPair != null)
            {
                throw new ArgumentOutOfRangeException();
            }

            if (pageType == Goal.Modify && wordPair == null)
            {
                throw new ArgumentOutOfRangeException();
            }

            PageType = pageType;

            if (wordPair == null)
            {
                this._currentWordPair = WordPair.NewEmpty();
            }
            else
            {
                this._currentWordPair = wordPair;
            }

            this._initialWordPair = _currentWordPair.CloneTo();

            CanBeAddedToUserLibrary = canBeAddedToUserLibrary;
            AddToUserLibrary = CanBeAddedToUserLibrary;

            ConfirmCommand = new RelayCommand(Confirm);
            CancelCommand = new RelayCommand(Cancel);
        }

        public string MajorityWord 
        { 
            get { return _currentWordPair.MajorityWord; }
            set
            {
                if (_currentWordPair.MajorityWord != value)
                {
                    _currentWordPair.MajorityWord = value;  
                }
            }
        }

        public string MinorityWord
        {
            get { return _currentWordPair.MinorityWord; }
            set
            {
                if (_currentWordPair.MinorityWord != value)
                {
                    _currentWordPair.MinorityWord = value;
                }
            }
        }

        public bool CanSwap
        {
            get { return _currentWordPair.CanSwap; }
            set
            {
                if (_currentWordPair.CanSwap != value)
                {
                    _currentWordPair.CanSwap = value;
                }
            }
        }

        public ICommand ConfirmCommand { get; private set; }

        public ICommand CancelCommand { get; private set; }

        public enum Goal { CreateNew, Modify }
        public Goal PageType { get; private set; }

        public enum ResultStatus { OK, Cancel }
        public ResultStatus Result { get; private set; }

        public bool CanBeAddedToUserLibrary { get; private set; }
        public bool AddToUserLibrary { get; private set; }

        private void Confirm()
        {
            if (String.IsNullOrEmpty(MajorityWord))
            {
                MessageBox.Show("人的词语不能为空");
                return;
            }

            if (String.IsNullOrEmpty(MinorityWord))
            {
                MessageBox.Show("傻子的词语不能为空");
                return;
            }

            if (MajorityWord == MinorityWord)
            {
                MessageBox.Show("人与傻子的词语不能一样");
                return;
            }

            if (MajorityWord.Length != MinorityWord.Length)
            {
                MessageBox.Show("人与傻子的词语长度必须一样");
                return;
            }

            if (_initialWordPair != null && _initialWordPair.Equals(_currentWordPair))
            {
                MessageBox.Show("词汇没有变化");
                return;
            }

            if (CanBeAddedToUserLibrary && AddToUserLibrary)
            {
                // to do : add the pair into the user library if it's not duplicated to some one.
            }            

            Result = ResultStatus.OK;
            ServiceLocator.Current.GetInstance<INavigationService>().GoBack();
        }

        private void Cancel()
        {
            _currentWordPair.Set(_initialWordPair);
            Result = ResultStatus.Cancel;
            ServiceLocator.Current.GetInstance<INavigationService>().GoBack();
        }

        private WordPair _currentWordPair;
        private WordPair _initialWordPair;
    }
}
